﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using HotelMgr.Models;
using System.Data;
using System.Data.Entity;
using HotelMgr.ViewModels;

namespace HotelMgr.Controllers
{
    [Authorize(Roles = "Employees")]
    public class OrderPanelBarController : Controller
    {
        readonly HotelEntities _db = new HotelEntities();
        //
        // GET: /OrderPanelBar/

        public ActionResult Index()
        {
            var orders = _db.Orders.ToList();
            return View(orders);
        }

        /// <summary>
        /// 房间信息
        /// </summary>
        /// <returns></returns>
        public ActionResult RoomInfo()
        {
            var roomTypes = _db.RoomTypes;
            var roomStates = _db.RoomStates;
            var rooms = _db.Rooms.Include(r => r.RoomType).Include(r => r.RoomState);
            var vmSearchRoom = new VMOrderRoomList()
                                    {
                                        RoomTypes = roomTypes.ToList(),
                                        RoomStates = roomStates.ToList(),
                                        Rooms = rooms.ToList()
                                    };
            return View(vmSearchRoom);
        }

        [HttpPost]
        public ActionResult RoomInfo(List<int> roomTypeFilter, List<int> roomStateFilter, int? roomNum_lower, int? roomNum_upper, decimal? price_lower, decimal? price_upper)
        {
            var rooms = _db.Rooms.Include(r => r.RoomType).Include(r => r.RoomState);
            rooms = rooms.Where(r => roomTypeFilter.Contains(r.RoomTypeID) && roomStateFilter.Contains(r.RoomStateID));
            if (roomNum_lower.HasValue)
            {
                rooms = rooms.Where(r => r.RoomID >= roomNum_lower.Value);
            }
            if (roomNum_upper.HasValue)
            {
                rooms = rooms.Where(r => r.RoomID <= roomNum_upper.Value);
            }
            if (price_lower.HasValue)
            {
                rooms = rooms.Where(r => r.RoomPrice >= price_lower);
            }
            if (price_upper.HasValue)
            {
                rooms = rooms.Where(r => r.RoomPrice <= price_upper);
            }
            return PartialView("RoomList", rooms.ToList());
        }

        public ActionResult Create()
        {
            //初始化订单
            var order = new Order
                            {
                                Customer = new Customer {CustomerID = Guid.NewGuid()}, 
                                CheckInTime = DateTime.Now
                            };
            return View(order);
        }

        [HttpPost]
        public ActionResult Create(Order orderToAdd, string txtRoomList)
        {
            if (ModelState.IsValid)
            {
                var roomList = txtRoomList.Split(new char[] {';'}, StringSplitOptions.RemoveEmptyEntries);
                if (roomList.Any() && orderToAdd.Rooms==null)
                {
                    orderToAdd.Rooms = new List<Room>();
                }
                foreach (var roomID in roomList)
                {
                    try
                    {
                        var room = _db.Rooms.Find(Convert.ToInt32(roomID));
                        orderToAdd.Rooms.Add(room);
                    }
                    catch{}
                }
                orderToAdd.Status = "未付款";
                _db.Orders.Add(orderToAdd);
                var aa = _db.SaveChanges();
                return View();
            }
            return RedirectToAction("Index");
        }

        /// <summary>
        /// 保存订单，付款页面
        /// </summary>
        /// <param name="id">订单ID</param>
        /// <returns></returns>
        public ActionResult SaveOrder(int id, bool isNew=false)
        {
            var order = _db.Orders.Find(id);
            ViewBag.IsNew = isNew;
            return View(order);
        }

        /// <summary>
        /// 确认付款
        /// </summary>
        /// <param name="id">订单ID</param>
        /// <returns></returns>
        public ActionResult ConfirmOrder(int id)
        {
            var order = _db.Orders.Find(id);
            order.Status = "已付款";
            foreach (var room in order.Rooms)
            {
                room.RoomStateID = 2;
            }
            _db.Entry(order).State = EntityState.Modified;
            _db.SaveChanges();
            return RedirectToAction("SaveOrder", new {id = id});
        }

        public ActionResult CancelOrder(int id)
        {
            var order = _db.Orders.Find(id);
            _db.Orders.Remove(order);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }
        public ActionResult CheckOut(int id)
        {
            var order = _db.Orders.Find(id);
            order.Status = "已退房";
            foreach (var room in order.Rooms)
            {
                room.RoomStateID = 1;
            }
            _db.Entry(order).State=EntityState.Modified;
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Statistik()
        {
            return View();
        }

    }
}
